﻿using System;
using System.Drawing;
using System.Windows.Forms;

namespace SuperCalculator
{
    /// <summary>
    /// 用于放置函数按钮的面板。
    /// </summary>
    internal sealed class ButtonsLayoutPanel : Panel
    {
        private const int dxButtonLoc = 6;
        private const int dyButtonLoc = 6;
        private const int xButtonCount = 4;

        public ButtonsLayoutPanel()
        {
            AutoScroll = true;
        }

        /// <summary>
        /// 添加函数按钮到面板。
        /// </summary>
        /// <param name="btn">要添加的按钮对象。</param>
        public void AddButton(ref Button btn)
        {
            //计算按钮大小
            btn.Width = (Width - (xButtonCount - 1) * dxButtonLoc) / xButtonCount;
            btn.Height = Convert.ToInt32(btn.Width / 2.5);
            //计算按钮位置
            var index = Controls.Count;
            var rowLoc = index / xButtonCount;
            var columnLoc = index % xButtonCount;
            btn.Location = new Point(columnLoc * (btn.Width + dxButtonLoc), rowLoc * (btn.Height + dyButtonLoc));
            if (!Contains(btn)) Controls.Add(btn);
        }
    }
}
